// CESEthnicity
log using CESEthnicity.log, replace
use "C:\Users\sbstjp\OneDrive - Cardiff University\CES24_Common.dta" // Schaffner, Brian; Shih, Marissa; Ansolabehere, Stephen; Pope, Jeremy, 2025, "Cooperative Election Study Common Content, 2024", https://doi.org/10.7910/DVN/X11EP6, Harvard Dataverse, V3. Accessed on 12 April 2025.

// Social Justice scale
rename CC24_321e defund
rename CC24_444a gendertransition
rename CC24_444b parentalconsentpronouns 
rename CC24_445a raceuniadmissions

*Reverse coding so social justice values are high
foreach var in defund {
    qui sum `var'
    local max_value = r(max)
    gen r`var' = `max_value' + 1 - `var'
}

* Replace missing values with 0 for the specified variables - this is necessary as Stata doesn't add up missing values and means a 0-1 standardization scale isn't feasible as missing values would overlap with the scale
foreach var in rdefund parentalconsentpronouns gendertransition raceuniadmissions {
    replace `var' = 0 if missing(`var')
}

* Initialize the total score and the count of non-zero responses
gen total_scoreSJV = 0
gen count_nonzeroSJV = 0

* Add each variable to the total scale score and count it if non-zero
foreach var in rdefund parentalconsentpronouns gendertransition raceuniadmissions  {
    replace total_scoreSJV = total_scoreSJV + `var'
    replace count_nonzeroSJV = count_nonzeroSJV + (`var' != 0)
}

* Calculate the average score, avoiding division by zero
gen SocJusValues = .
replace SocJusValues = total_scoreSJV / count_nonzeroSJV if count_nonzeroSJV > 0

// Demographics
*Delete missing values and rename
replace gender4=. if gender4>2
rename gender4 FemaleGender

replace faminc_new=. if faminc_new==97 

generate age = 2024 - birthyr

*Create dummies
gen Graduate=.
replace Graduate=0 if inrange(educ, 1, 4)
replace Graduate=1 if inlist(educ, 5, 6)

gen White=.
replace White=1 if race==1
replace White=0 if inrange(race, 2, 8)

gen Black=.
replace Black=1 if race==2
replace Black=0 if race==1
replace Black=0 if inrange(race, 3, 8)

gen Hispanic=.
replace Hispanic=1 if race==3
replace Hispanic=0 if inlist(race, 1, 2)
replace Hispanic=0 if inrange(race, 4, 8)

gen Asian=.
replace Asian=1 if race==4
replace Asian=0 if inrange(race, 1, 3)
replace Asian=0 if inrange(race, 5, 8)

gen Mixed=.
replace Mixed=1 if race==6
replace Mixed=0 if inrange(race, 1, 5)
replace Mixed=0 if inlist(race, 7, 8)

// Standardize
egen Age = std(age)
egen Income = std(faminc_new)

// Regessions
regress SocJusValues Age FemaleGender Graduate Income White [pweight=commonweight], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income Black [pweight=commonweight], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income Hispanic [pweight=commonweight], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income Asian [pweight=commonweight], robust 
eststo
regress SocJusValues Age FemaleGender Graduate Income Mixed [pweight=commonweight], robust 
eststo
esttab

//Correlations
pwcorr SocJusValues White Black Hispanic Asian Mixed [aweight=commonweight], sig

log close
